﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;

using Microsoft.WindowsAPICodePack.Shell;

namespace BackupGUI
{
    using BackupLib;

    /// <summary>
    /// Interaktionslogik für TaskSettings.xaml
    /// </summary>
    public partial class TaskSettings : Window
    {
        public TaskSettings()
        {
            InitializeComponent();
            btnSelectSource.DataContext = txtSource;
            btnSelectTarget.DataContext = txtTarget;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            txtSource.GetBindingExpression(TextBox.TextProperty).UpdateSource();    
            txtTarget.GetBindingExpression(TextBox.TextProperty).UpdateSource();    
            txtInclude.GetBindingExpression(TextBox.TextProperty).UpdateSource();    
            txtExclude.GetBindingExpression(TextBox.TextProperty).UpdateSource();    
            chkACL.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();    
            chkADS.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();    
            chkFullBackup.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();    
            chkRecursive.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();    
            chkHidden.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();    
            chkSystem.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();    
            if (chkCreated.IsChecked==true)
            {

                createdFrom.GetBindingExpression(DatePicker.SelectedDateProperty).UpdateSource();
                createdTo.GetBindingExpression(DatePicker.SelectedDateProperty).UpdateSource();
            }
            else
            {
                BindingOperations.GetMultiBindingExpression(chkCreated,CheckBox.IsCheckedProperty).UpdateSource();
            }
            if (chkModified.IsChecked==true)
            {
                modifiedFrom.GetBindingExpression(DatePicker.SelectedDateProperty).UpdateSource();
                modifiedTo.GetBindingExpression(DatePicker.SelectedDateProperty).UpdateSource();
            }      
            else
            {
                BindingOperations.GetMultiBindingExpression(chkModified, CheckBox.IsCheckedProperty).UpdateSource();
            }
            if (chkSize.IsChecked==true)
            {
                txtMinSize.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                txtMaxSize.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            }
             else
            {
                BindingOperations.GetMultiBindingExpression(chkSize, CheckBox.IsCheckedProperty).UpdateSource();
            }
            Close();
        }

        private List<TextBox> EnumTextBox(Visual visual)
        {
            List<TextBox> result=new List<TextBox>();
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
            {
                Visual childVisual = (Visual)VisualTreeHelper.GetChild(visual, i);
                if (childVisual is TextBox)
                {
                    result.Add(childVisual as TextBox);
                }
            }
            return result;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void createdFrom_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void txtSource_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btnSender = (Button) sender;
            TextBox txt = (TextBox) btnSender.DataContext;

            CommonOpenFileDialog folderDialog=new CommonOpenFileDialog();
            folderDialog.FoldersOnly = true;
            folderDialog.Title = "Pfad wählen";
            folderDialog.InitialDirectory = txt.Text;

            if (folderDialog.ShowDialog().Canceled==false)
            {
                txt.Text = folderDialog.FileName;
            }
        }
    }
}
